package com.llsydn.ui.listview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.llsydn.hello.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title listView
 * @description 请填写相关描述
 * @date 2018/9/21 10:21
 */
public class ListViewActivity extends AppCompatActivity {

    private ListView lv_main;
    private List<ShopInfo> data;

    /**
     * ArrayAdapter:  显示最简单的列表(文本)
     * 集合数据为List<String>或String[]
     * SimpleAdapter: 显示复杂的列表
     * 集合数据必须是List<Map<String,Object>>类型
     * BaseAdapter: 显示复杂的列表
     * 集合数据可以是任意类型的集合List<Xxx>
     * SimpleCursorAdapter: 显示复杂的列表
     * 集合数据是数据库查询结果集
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        lv_main = findViewById(R.id.lv_main);

        //arrayAdapter();
        //simpleAdapter();
        baseAdapter();
    }

    /**
     * （一）ArrayAdapter:  显示最简单的列表(文本)
     */
    public void arrayAdapter() {
        //准备集合数据
        String[] data = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"};
        //准备ArrayAdapter对象
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_array_adapter, data);
        //设置adapter对象
        lv_main.setAdapter(adapter);
    }

    /**
     * （二）SimpleAdapter:  显示复杂的列表
     */
    public void simpleAdapter() {
        //准备集合数据
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(16);
        Map<String, Object> map = new HashMap<String, Object>(16);

        for (int i = 1; i < 11; i++) {
            map = new HashMap<String, Object>(16);
            if (i == 1) {
                map.put("icon", R.drawable.f1);
            }
            if (i == 2) {
                map.put("icon", R.drawable.f2);
            }
            if (i == 3) {
                map.put("icon", R.drawable.f3);
            }
            if (i == 4) {
                map.put("icon", R.drawable.f4);
            }
            if (i == 5) {
                map.put("icon", R.drawable.f5);
            }
            if (i == 6) {
                map.put("icon", R.drawable.f6);
            }
            if (i == 7) {
                map.put("icon", R.drawable.f7);
            }
            if (i == 8) {
                map.put("icon", R.drawable.f8);
            }
            if (i == 9) {
                map.put("icon", R.drawable.f9);
            }
            if (i == 10) {
                map.put("icon", R.drawable.f10);
            }
            map.put("name", "name---" + i);
            map.put("content", "content---" + i);
            data.add(map);
        }

        //准备ArrayAdapter对象
        //from: map对象中的key的数组, 用于得到对应的value
        String[] from = {"icon", "name", "content"};
        //Item布局文件中的子view的id的数组
        int[] to = {R.id.iv_item_icon, R.id.tv_item_name, R.id.tv_item_content};
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter, from, to);
        //设置adapter对象
        lv_main.setAdapter(adapter);
    }


    /**
     * （三）BaseAdapter: 显示复杂的列表
     */
    public void baseAdapter() {
        //准备数据
        data = new ArrayList<ShopInfo>(16);
        data.add(new ShopInfo(R.drawable.f1, "name----1", "content----1"));
        data.add(new ShopInfo(R.drawable.f2, "name----2", "content----2"));
        data.add(new ShopInfo(R.drawable.f3, "name----3", "content----3"));
        data.add(new ShopInfo(R.drawable.f4, "name----4", "content----4"));
        data.add(new ShopInfo(R.drawable.f5, "name----5", "content----5"));
        data.add(new ShopInfo(R.drawable.f6, "name----6", "content----6"));
        data.add(new ShopInfo(R.drawable.f7, "name----7", "content----7"));
        data.add(new ShopInfo(R.drawable.f8, "name----8", "content----8"));
        data.add(new ShopInfo(R.drawable.f9, "name----9", "content----9"));
        data.add(new ShopInfo(R.drawable.f10, "name----10", "content----10"));

        //准备BaseAdapter对象
        MyBaseAdapter adapter = new MyBaseAdapter();

        //设置Adapter显示列表
        lv_main.setAdapter(adapter);
    }

    class MyBaseAdapter extends BaseAdapter {
        //返回集合数据的数量
        @Override
        public int getCount() {
            return data.size();
        }

        //返回指定下标对应的数据对象
        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        //返回指定下标所对应的item的view对象
        //position：下标，convertView：可复用的缓存Item视图对象，前n+1个为null，parent：ListView对象
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //如果没有复用的
            if (convertView == null) {
                //加载item的布局，得到view对象。
                convertView = View.inflate(ListViewActivity.this, R.layout.item_simple_adapter, null);
            }
            //根据position设置对应的数据
            ShopInfo shopInfo = data.get(position);
            ImageView imageIcon = convertView.findViewById(R.id.iv_item_icon);
            TextView textName = convertView.findViewById(R.id.tv_item_name);
            TextView textContent = convertView.findViewById(R.id.tv_item_content);

            imageIcon.setImageResource(shopInfo.getIcon());
            textName.setText(shopInfo.getName());
            textContent.setText(shopInfo.getContent());
            return convertView;
        }
    }

    class ShopInfo {
        private int icon;
        private String name;
        private String content;

        public ShopInfo(int icon, String name, String content) {
            this.icon = icon;
            this.name = name;
            this.content = content;
        }

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }

}
